<?php

namespace App\Admin\Controllers;

use App\Models\Message;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class MessageController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('用户消息')
            ->description('用户消息')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id, Content $content)
    {
        return $content
            ->header('Detail')
            ->description('description')
            ->body($this->detail($id));
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('Edit')
            ->description('description')
            ->body($this->form()->edit($id));
    }

    /**
     * Create interface.
     *
     * @param Content $content
     * @return Content
     */
    public function create(Content $content)
    {
        return $content
            ->header('Create')
            ->description('description')
            ->body($this->form());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Message);

        $grid->id('Id');
        // $grid->user_id('User id');
        $grid->phone('手机号');
        $grid->nickname('昵称');
        $grid->content('留言内容');
        $grid->reply('回复内容');
        $grid->status('状态')->display(function($value){
            return ($value==1)?"<span class='label label-warning'>待回复</span>":"<span class='label label-success'>已回复</span>";
        });
        $grid->created_at('留言时间');
        $grid->updated_at('更新时间');

        $grid->disableActions();

        $grid->filter(function($filter){
            $filter->disableIdFilter();
            $filter->equal('status', '状态')->radio([
                1 => '已回复',
                2 => '未回复',
            ]);
            // $filter->scope('待审核')->where('status', 0);
            $filter->scope('今日新增', '今日新增')->whereDate('created_at', date('Y-m-d'));
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(Message::findOrFail($id));

        $show->id('Id');
        $show->user_id('User id');
        $show->phone('Phone');
        $show->nickname('Nickname');
        $show->content('Content');
        $show->reply('Reply');
        $show->status('Status');
        $show->created_at('Created at');
        $show->updated_at('Updated at');

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Message);

        $form->number('user_id', 'User id');
        $form->mobile('phone', 'Phone');
        $form->text('nickname', 'Nickname');
        $form->textarea('content', 'Content');
        $form->textarea('reply', 'Reply');
        $form->number('status', 'Status');

        return $form;
    }
}
